<?php
declare (strict_types=1);

namespace app\system\controller;

use app\system\BaseController;
use app\service\MarkerService;

class MarkerCategory extends BaseController
{
    private MarkerService $service;

    public function initialize()
    {
        parent::initialize();
        $this->service = new MarkerService();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return $this->success($this->service->categoryList($this->request->get()));
    }


    public function dict()
    {
        return $this->success($this->service->categoryDict($this->request->get()));
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save()
    {
        $data = $this->request->only([
            'name',
            'icon_default_id',
            'icon_active_id',
            'icon_on_marker_id',
        ], 'post');
        $this->validate($data, [
            'name' => 'require|max:10',
            'icon_default_id' => 'require|integer',
            'icon_active_id' => 'require|integer',
            'icon_on_marker_id' => 'require|integer',
        ]);
        return $this->success($this->service->categorySave($data));
    }

    /**
     * 显示指定的资源
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!intval($id)) {
            return $this->error('id不能为空');
        }
        if (!$model = $this->service->categoryRead($id)) {
            return $this->error('详情不存在');
        }
        return $this->success($model);
    }


    /**
     * 保存更新的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function update($id)
    {
        if (!intval($id)) {
            return $this->error('id不能为空');
        }
        $data = $this->request->put();
        $this->validate($data, [
            'name' => 'require|max:10',
            'icon_default_id' => 'require|integer',
            'icon_active_id' => 'require|integer',
        ]);
        $data['id'] = $id;
        [$isOk, $result] = $this->service->categoryUpdate($data);
        return $isOk ? $this->success($result) : $this->error($result);
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $ids = $id ?: $this->request->delete('ids');
        if (!$ids) {
            return $this->error('id不能为空');
        }
        [$isOk, $result] = $this->service->categoryDel($ids);
        return $isOk ? $this->success($result) : $this->error($result);
    }
}
